package com.fast.fast.common.gateway.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fast.fast.common.base.constant.CacheConstants;
import com.fast.fast.common.gateway.entity.SysPerm;
import com.fast.fast.common.gateway.entity.SysRolePerm;
import com.fast.fast.common.gateway.entity.SysUserRole;
import com.fast.fast.common.gateway.mapper.SysPermMapper;
import com.fast.fast.common.gateway.service.SysPermService;
import com.fast.fast.common.gateway.service.SysRolePermService;
import com.fast.fast.common.gateway.service.SysUserRoleService;
import com.fast.fast.common.gateway.util.TreeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lyf
 * @description 针对表【sys_perm(系统权限表)】的数据库操作Service实现
 * @createDate 2022/01/01 00:00 周六
 */
@RequiredArgsConstructor
@Service
public class SysPermServiceImpl extends ServiceImpl<SysPermMapper, SysPerm>
        implements SysPermService {

    private final SysUserRoleService sysUserRoleService;

    private final SysRolePermService sysRolePermService;

    @Cacheable(cacheNames = CacheConstants.GATEWAY_LOGIN_SYS_PERM_CODE_USERID)
    @Override
    public List<String> listPermCodesByLoginId(Long userId) {
        List<SysPerm> sysPerms = this.listPermByLoginId(userId);
        if (ObjectUtil.isEmpty(sysPerms)) {
            return CollUtil.newArrayList();
        }
        List<String> sysPermCodes = sysPerms
                .stream()
                .map(SysPerm::getCode)
                .collect(Collectors.toList());
        return sysPermCodes;
    }

    @Cacheable(cacheNames = CacheConstants.GATEWAY_LOGIN_SYS_PERM_USERID)
    @Override
    public List<SysPerm> listPermObjsByLoginId(Long userId) {
        List<SysPerm> sysPerms = this.listPermByLoginId(userId);
        if (ObjectUtil.isEmpty(sysPerms)) {
            return CollUtil.newArrayList();
        }
        // 获取包括所有子节点信息的父节点
        List<SysPerm> nodes = sysPerms
                .stream()
                .filter(o -> o.getPid() == 0)
                .map(o -> TreeUtils.listNodes(sysPerms, o))
                .collect(Collectors.toList());
        return nodes;
    }

    /**
     * 获取用户拥有的权限对象集合（无父子结构）
     *
     * @param userId
     * @return
     */
    private List<SysPerm> listPermByLoginId(Long userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return CollUtil.newArrayList();
        }

        // 根据userId,获取roleIds
        List<Long> roleIds = sysUserRoleService.list(
                Wrappers.lambdaQuery(SysUserRole.class)
                        .eq(SysUserRole::getUserId, userId)
        ).stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        if (ObjectUtil.isEmpty(roleIds)) {
            return CollUtil.newArrayList();
        }

        // 根据roleIds,获取permIds
        List<Long> permIds = sysRolePermService.list(
                Wrappers.lambdaQuery(SysRolePerm.class)
                        .in(ObjectUtil.isNotEmpty(roleIds), SysRolePerm::getRoleId, roleIds)
        ).stream().map(SysRolePerm::getPermId).collect(Collectors.toList());
        if (ObjectUtil.isEmpty(permIds)) {
            return CollUtil.newArrayList();
        }

        // 根据permIds,获取sysPerms
        List<SysPerm> sysPerms = list(
                Wrappers.lambdaQuery(SysPerm.class)
                        .eq(SysPerm::getDisabled, 0)
                        .in(CollUtil.isNotEmpty(permIds), SysPerm::getId, permIds)
                        .orderByAsc(SysPerm::getSort)
        );

        return sysPerms;
    }

}




